﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

[assembly: WebResource("com.cruxomatic.items.navigationstrip.RollOverImageButton.RollOverImageButton.js", 
    "text/javascript")]
[assembly: ScriptResource("com.cruxomatic.items.navigationstrip.RollOverImageButton.RollOverImageButton.js",
   "com.cruxomatic.items.navigationstrip.RollOverImageButton", "com.cruxomatic.items.navigationstrip.Resource")]
namespace com.cruxomatic.items.navigationstrip
{
    public class RollOverImageButton : ImageButton, IScriptControl
    {
        private ScriptManager sm;
        private String overImageUrl;

        public RollOverImageButton()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        [Bindable(false)]
        [Category("Target Control Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public String OverImageUrl
        {
            get { return overImageUrl; }
            set { overImageUrl = value; }
        }


        #region Pre-Render and Render Overrides

        protected override void OnPreRender(EventArgs e)
        {
            if( !this.DesignMode )
            {
                sm = ScriptManager.GetCurrent(Page);
                if( sm == null )
                    throw new HttpException("A script manager control must exist in the page");
                sm.RegisterScriptControl(this);
            }
            
            base.OnPreRender(e);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if( !this.DesignMode )
            {
                sm.RegisterScriptDescriptors(this);
            }
            
            base.Render(writer);
        }

        #endregion

        #region IScriptControl Implementation

        // Generate the script descriptors
        IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
        {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor("com.cruxomatic.items.navigationstrip.RollOverImageButton", this.ClientID);
            descriptor.AddProperty("overImageUrl", this.OverImageUrl);
            descriptor.AddProperty("imageUrl", this.ImageUrl);
            yield return descriptor;
        }

        // Generate the script reference
        IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
        {
            yield return new ScriptReference("com.cruxomatic.items.navigationstrip.RollOverImageButton.RollOverImageButton.js", this.GetType().Assembly.FullName);
        }

        #endregion
    }
}